package cp.remote.activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import cp.remote.R;
import cp.remote.config.KinectConfig;
import cp.remote.config.NetworkConfig;
import cp.remote.controller.ManuallCarControll;
import cp.remote.network.RemoteNetWorker;
import cp.remote.network.TaskSwitch;
import cp.remote.timer.KinectTimer;

public class Remote_Activity extends Main_Activity {

    private KinectTimer t = null;
	private RemoteNetWorker rem;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		rem = new RemoteNetWorker();
		rem.start();
		NetworkConfig.nettask.send(TaskSwitch.MANUAL);
		setContentView(R.layout.remote);
		ManuallCarControll listener = new ManuallCarControll(rem, this);
		
		SeekBar temp = (SeekBar) findViewById(R.id.remote_seek_speed);
		temp.setMax(100);
		temp.setOnSeekBarChangeListener(listener);

		((ImageButton) findViewById(R.id.remote_button_stop))
				.setOnClickListener(listener);
		((ImageButton) findViewById(R.id.remote_button_up_l))
				.setOnTouchListener(listener);
		((ImageButton) findViewById(R.id.remote_button_down_l))
				.setOnTouchListener(listener);
		((ImageButton) findViewById(R.id.remote_button_left_l))
				.setOnTouchListener(listener);
		((ImageButton) findViewById(R.id.remote_button_up_r))
				.setOnTouchListener(listener);
		((ImageButton) findViewById(R.id.remote_button_down_r))
				.setOnTouchListener(listener);
		((ImageButton) findViewById(R.id.remote_button_right_r))
				.setOnTouchListener(listener);
		
        if (KinectConfig.kinectimage) {
            Handler myHandler = new Handler();
            t = new KinectTimer(myHandler,
                    (ImageView) findViewById(R.id.remote_video));
            myHandler.removeCallbacks(t);
            myHandler.postDelayed(t, 1000);
        }
	}

    @Override
    public void customStop() {
        if(t != null)
            t.stop(); 
        rem.stop();
        rem = null;       
    }
    
    public void setButtonImage(int buttonid, int resid){
        ((ImageButton)findViewById(buttonid)).setImageResource(resid);
    }
    
    public void setSpeedText(int value){
        ((TextView)findViewById(R.id.remote_txt_speed)).setText(String.format("%d", value));
    }
    
    public void setStateText(String txt){
        ((EditText)findViewById(R.id.remote_txt_state)).append(txt +"\n");
    }
}
